package md.mru.frontend.controllers;

import md.mru.core.domains.type.SystemTagType;
import md.mru.core.dtos.SystemTagDto;
import md.mru.core.service.AdministrationService;
import org.apache.log4j.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 31/05/2014.
 */
@ManagedBean
@ViewScoped
public class SystemTagAdministrationBean extends BackingBean implements Serializable {

    private static final long serialVersionUID = -462306583032625177L;

    Logger logger = Logger.getLogger(SystemTagAdministrationBean.class);

    @ManagedProperty(value = "#{administrationService}")
    private AdministrationService administrationService;

    private List<SystemTagDto> systemTags;
    private SystemTagDto selectedSystemTag;

    public void resetCreateSystemTagDialog() {
        this.selectedSystemTag = new SystemTagDto();
    }

    public void initializeSystemTagsContent() {
        systemTags = administrationService.getSystemTags();
    }


/*    public void addNewSystemTag() {
        this.selectedSystemTag = new SystemTagDto(SystemTagType.NO_SYSTEM_TAG);
    }*/

    public void saveSystemTag() {
        long systemTagId = administrationService.saveSystemTag(selectedSystemTag);
        if (selectedSystemTag.getId() == 0) {
            selectedSystemTag.setId(systemTagId);
            systemTags.add(selectedSystemTag);
        }
    }

    public void deleteSystemTag(SystemTagDto systemTag) {
        administrationService.deleteSystemTag(systemTag.getId());
        this.systemTags.remove(systemTag);
    }

    public boolean showSystemTagEdit(SystemTagDto systemTag) {
        return systemTag.getSystemTagType().equals(SystemTagType.CHANGE_VALUE_SYSTEM_TAG);
    }

    public boolean noSystemTagType() {
        return this.noSystemTagType(selectedSystemTag);
    }

    public boolean noSystemTagType(SystemTagDto systemTag) {
        return systemTag.getSystemTagType().equals(SystemTagType.NO_SYSTEM_TAG);
    }

    public void setSelectedSystemTag(SystemTagDto selectedSystemTag) {
        this.selectedSystemTag = selectedSystemTag;
    }

    public SystemTagDto getSelectedSystemTag() {
        return selectedSystemTag;
    }

    public List<SystemTagDto> getSystemTags() {
        return systemTags;
    }

    public void setAdministrationService(AdministrationService administrationService) {
        this.administrationService = administrationService;
    }
}
